package com.eleadmin.order.biz.impl;

import com.eleadmin.common.system.entity.User;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.order.biz.MaintainFeeAttachBiz;
import com.eleadmin.order.entity.MaintainFeeAttach;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.order.result.SelectResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-03-28 13:48:23
 */
@Repository
public class MaintainFeeAttachBizImpl extends BaseDao implements MaintainFeeAttachBiz {

    @Override
    public JsonResult list(PageResult2<T> page, MaintainFeeAttach maintainFeeAttach, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
        StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.name");
        sql.append(",m.pinyin,m.box_type,m.price,m.at_modules");
        sql.append(",m.longitude,m.latitude,m.radius,m.yard_dock");
        sql.append(",m.remark,m.create_time,m.create_user,m.update_time");
        sql.append(",m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_maintain_fee_attach m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=m.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and m.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(maintainFeeAttach)) {
            if (Utils.isNotEmpty(maintainFeeAttach.getName())) {
                sql.append(" and (instr(m.name,:name)>0 or instr(m.pinyin,:name)>0) ");
                map.put("name", maintainFeeAttach.getName());
            }
            if (Utils.isNotEmpty(maintainFeeAttach.getAtModules())) {
                sql.append(" and instr(m.at_modules,:at_modules)>0 ");
                map.put("at_modules", maintainFeeAttach.getAtModules());
            }
            if (Utils.isNotEmpty(maintainFeeAttach.getSort()) && Utils.isNotEmpty(maintainFeeAttach.getOrder())) {
                sql.append(" order by m." + maintainFeeAttach.getSort() + " " + maintainFeeAttach.getOrder());
            } else {
                sql.append(" order by m.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<MaintainFeeAttach> list(MaintainFeeAttach maintainFeeAttach, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
        StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.name");
        sql.append(",m.pinyin,m.box_type,m.price,m.at_modules");
        sql.append(",m.longitude,m.latitude,m.radius,m.yard_dock");
        sql.append(",m.remark,m.create_time,m.create_user,m.update_time");
        sql.append(",m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_maintain_fee_attach m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and m.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(maintainFeeAttach)) {
            if (Utils.isNotEmpty(maintainFeeAttach.getName())) {
                sql.append(" and (instr(m.name,:name)>0 or instr(m.pinyin,:name)>0) ");
                map.put("name", maintainFeeAttach.getName());
            }
            if (Utils.isNotEmpty(maintainFeeAttach.getAtModules())) {
                sql.append(" and instr(m.at_modules,:at_modules)>0 ");
                map.put("at_modules", maintainFeeAttach.getAtModules());
            }
            if (Utils.isNotEmpty(maintainFeeAttach.getSort()) && Utils.isNotEmpty(maintainFeeAttach.getOrder())) {
                sql.append(" order by m." + maintainFeeAttach.getSort() + " " + maintainFeeAttach.getOrder());
            } else {
                sql.append(" order by m.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSel(MaintainFeeAttach maintainFeeAttach, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select m.id value, m.name label,m.at_modules module, m.price");
        sql.append(" ,m.longitude,m.latitude ");
        sql.append(" from chf_maintain_fee_attach m ");
        sql.append(" where m.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and m.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(maintainFeeAttach)) {
            if (Utils.isNotEmpty(maintainFeeAttach.getName())) {
                sql.append(" and (instr(m.name,:name)>0 or instr(m.pinyin,:name)>0) ");
                map.put("name", maintainFeeAttach.getName());
            }
            if (Utils.isNotEmpty(maintainFeeAttach.getAtModules())) {
                sql.append(" and instr(m.at_modules,:at_modules)>0 ");
                map.put("at_modules", maintainFeeAttach.getAtModules());
            }
            if (Utils.isNotEmpty(maintainFeeAttach.getYardDock())) {
                sql.append(" and instr(m.yard_dock,:yard_dock)>0 ");
                map.put("yard_dock", maintainFeeAttach.getYardDock());
            }
            if (Utils.isNotEmpty(maintainFeeAttach.getSort()) && Utils.isNotEmpty(maintainFeeAttach.getOrder())) {
                sql.append(" order by m." + maintainFeeAttach.getSort() + " " + maintainFeeAttach.getOrder());
            } else {
                sql.append(" order by m.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public MaintainFeeAttach getById(String id) {
        RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.name");
        sql.append(",m.pinyin,m.box_type,m.price,m.at_modules");
        sql.append(",m.longitude,m.latitude,m.radius,m.yard_dock");
        sql.append(",m.remark,m.create_time,m.create_user,m.update_time");
        sql.append(",m.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_maintain_fee_attach m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=m.organization_id and sogzn.deleted=0 ");
        sql.append(" where m.deleted=0 and m.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(MaintainFeeAttach maintainFeeAttach, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_maintain_fee_attach (");
        sql.append("organization_id,name,pinyin,box_type");
        sql.append(",price,at_modules,longitude,latitude");
        sql.append(",radius,yard_dock,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:pinyin,:box_type");
        sql.append(",:price,:at_modules,:longitude,:latitude");
        sql.append(",:radius,:yard_dock,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", maintainFeeAttach.getName());
        map.put("pinyin", maintainFeeAttach.getPinyin());
        map.put("box_type", maintainFeeAttach.getBoxType());
        map.put("price", maintainFeeAttach.getPrice());
        map.put("at_modules", maintainFeeAttach.getAtModules());
        map.put("longitude", maintainFeeAttach.getLongitude());
        map.put("latitude", maintainFeeAttach.getLatitude());
        map.put("radius", maintainFeeAttach.getRadius() == null ? null : maintainFeeAttach.getRadius() + "");
        map.put("yard_dock", maintainFeeAttach.getYardDock());
        map.put("remark", maintainFeeAttach.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(MaintainFeeAttach maintainFeeAttach, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_maintain_fee_attach set ");
        sql.append("name=:name,pinyin=:pinyin,box_type=:box_type ");
        sql.append(",price=:price,at_modules=:at_modules,longitude=:longitude ");
        sql.append(",latitude=:latitude,radius=:radius,yard_dock=:yard_dock ");
        sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", maintainFeeAttach.getName());
        map.put("pinyin", maintainFeeAttach.getPinyin());
        map.put("box_type", maintainFeeAttach.getBoxType());
        map.put("price", maintainFeeAttach.getPrice());
        map.put("at_modules", maintainFeeAttach.getAtModules());
        map.put("longitude", maintainFeeAttach.getLongitude());
        map.put("latitude", maintainFeeAttach.getLatitude());
        map.put("radius", maintainFeeAttach.getRadius() == null ? null : maintainFeeAttach.getRadius() + "");
        map.put("yard_dock", maintainFeeAttach.getYardDock());
        map.put("remark", maintainFeeAttach.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", maintainFeeAttach.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_maintain_fee_attach set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
